package edu.yctc.weblog.controller.rest.impl;

import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import edu.yctc.weblog.constant.ResultCode;
import edu.yctc.weblog.constant.URLConstant;
import edu.yctc.weblog.controller.rest.CategoryRestController;
import edu.yctc.weblog.entity.PageDO;
import edu.yctc.weblog.entity.ResultDO;
import edu.yctc.weblog.service.CategoryService;
import edu.yctc.weblog.utils.PageQueryUtil;
import edu.yctc.weblog.vo.CategoryVO;
import edu.yctc.weblog.vo.UpdateCategoryVO;

@RestController
@RequestMapping(URLConstant.S + URLConstant.CATEGORY)
public class CategoryRestControllerImpl implements CategoryRestController {

    @Autowired
    private CategoryService categoryService;

    @Override
    @GetMapping(URLConstant.CATEGORY_GET_CATEGORY_LIST)
    public ResultDO<PageDO> getCategoryList(@RequestParam Map<String, Object> params) {
        if (params.get("page") == null || params.get("limit") == null) {
            return new ResultDO<PageDO>(false, ResultCode.PARAMETER_INVALID, ResultCode.MSG_PARAMETER_INVALID, null);
        }
        return categoryService.getCategoryList(new PageQueryUtil(params));
    }

    @Override
    @PostMapping(URLConstant.CATEGORY_SAVE_CATEGORY)
    public ResultDO<Void> saveCategory(@ModelAttribute CategoryVO categoryVO) {
        if (StringUtils.isBlank(categoryVO.getCategoryName()) || StringUtils.isBlank(categoryVO.getCategoryIcon())) {
            return new ResultDO<>(false, ResultCode.PARAMETER_INVALID, ResultCode.MSG_PARAMETER_INVALID);
        }
        return categoryService.saveCategory(categoryVO.getCategoryName(), categoryVO.getCategoryIcon());
    }

    @Override
    @PostMapping(URLConstant.CATEGORY_UPDATE_CATEGORY)
    public ResultDO<Void> updateCategory(@ModelAttribute UpdateCategoryVO updateCategoryVO) {
        if (updateCategoryVO.getCategoryId() < 0 || StringUtils.isBlank(updateCategoryVO.getCategoryName())
            || StringUtils.isBlank(updateCategoryVO.getCategoryIcon())) {
            return new ResultDO<>(false, ResultCode.PARAMETER_INVALID, ResultCode.MSG_PARAMETER_INVALID);
        }
        return categoryService.updateCategory(updateCategoryVO.getCategoryId(), updateCategoryVO.getCategoryName(),
            updateCategoryVO.getCategoryIcon());
    }

    @Override
    @PostMapping(URLConstant.CATEGORY_DELETE_CATEGORY)
    public ResultDO<Void> deleteCategory(@RequestBody Long[] ids) {
        if (ids.length < 1) {
            return new ResultDO<>(false, ResultCode.PARAMETER_INVALID, ResultCode.MSG_PARAMETER_INVALID);
        }
        return categoryService.deleteCategory(ids);
    }

}
